package me.imli.app.xml;

import java.io.IOException;
import java.io.InputStream;

import javax.xml.parsers.ParserConfigurationException;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;

import me.imli.app.model.RssFeedModel;
import me.imli.app.model.RssItemModel;

import org.xml.sax.Attributes;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
import org.xml.sax.XMLReader;
import org.xml.sax.helpers.DefaultHandler;

public class RSSHandler extends DefaultHandler {
	
	// KEY
	private final static String KEY_RSS = "rss";
	private final static String KEY_CHANNEL = "channel";
	private final static String KEY_ITEM = "item";
	private final static String KEY_TITLE = "title";
	private final static String KEY_LINK = "link";
	private final static String KEY_PUBDATE = "pubDate";
	private final static String KEY_GUID = "guid";
	private final static String KEY_DESCRIPTION = "description";
	
	// 类型枚举
	private int mType = TYPE_NONE;
	private final static int TYPE_NONE = 0;
	private final static int TYPE_RSS = 1;
	private final static int TYPE_CHANNEL = 2;
	private final static int TYPE_ITEM = 3;
	
	/**
	 * 解析RSS-XML
	 * @param stream
	 * @return
	 */
	public static RssFeedModel parseRssXml(InputStream stream) {
		try {
			SAXParserFactory factory = SAXParserFactory.newInstance();
			SAXParser parser = factory.newSAXParser();
			XMLReader reader = parser.getXMLReader();
			RSSHandler handler = new RSSHandler();
			reader.setContentHandler(handler);
			reader.parse(new InputSource(stream));
			return handler.mFree;
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (SAXException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (ParserConfigurationException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return null;
	}

	private String mContent;

	private RssFeedModel mFree;
	private RssItemModel mItem;


	@Override
	public void startDocument() throws SAXException {
		super.startDocument();
	}

	@Override
	public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException {
		if (localName.equals(KEY_RSS)) {
			mType = TYPE_RSS;
		} else if (localName.equals(KEY_CHANNEL)) {
			mType = TYPE_CHANNEL;
			mFree = new RssFeedModel();
		} else if (localName.equals(KEY_ITEM)) {
			mType = TYPE_ITEM;
			mItem = new RssItemModel();
		} else if (localName.equals(KEY_TITLE) | localName.equals(KEY_DESCRIPTION)
				| localName.equals(KEY_GUID) | localName.equals(KEY_PUBDATE)
				| localName.equals(KEY_LINK)) {
			mContent = "";
		}
	}

	@Override
	public void characters(char[] ch, int start, int length) throws SAXException {
		mContent += new String(ch, start, length);
	}

	@Override
	public void endElement(String uri, String localName, String qName) throws SAXException {
		if (localName.equals(KEY_RSS)) {
			mType = TYPE_NONE;
		} else if (localName.equals(KEY_CHANNEL)) {
			mType = TYPE_RSS;
		} else if (localName.equals(KEY_ITEM)) {
			mType = TYPE_CHANNEL;
			mFree.items.add(mItem);
		} else if (localName.equals(KEY_TITLE)) {
			if (mType == TYPE_CHANNEL) {
				mFree.title = mContent;
			} else if (mType == TYPE_ITEM) {
				mItem.title = mContent;
			}
		} else if (localName.equals(KEY_LINK)) {
			if (mType == TYPE_CHANNEL) {
				mFree.link = mContent;
			} else if (mType == TYPE_ITEM) {
				mItem.link = mContent;
			}
		} else if (localName.equals(KEY_PUBDATE)) {
			if (mType == TYPE_CHANNEL) {
				mFree.pubDate = mContent;
			} else if (mType == TYPE_ITEM) {
				mItem.pubDate = mContent;
			}
		} else if (localName.equals(KEY_GUID)) {
			if (mType == TYPE_CHANNEL) {
			} else if (mType == TYPE_ITEM) {
				mItem.guid = mContent;
			}
		} else if (localName.equals(KEY_DESCRIPTION)) {
			if (mType == TYPE_CHANNEL) {
				mFree.description = mContent;
			} else if (mType == TYPE_ITEM) {
				mItem.description = mContent;
			}
		}
	}
	
	@Override
	public void endDocument() throws SAXException {
		super.endDocument();
	}

}
